package site.qjdb.config.schedule;

import site.qjdb.dao.KillSlowSqlDao;
import site.qjdb.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

@Slf4j
@Configuration
@EnableAsync
@EnableScheduling
public class KillSlowSqlSchedule {
    @Resource
    private KillSlowSqlDao dao;

    /**
     * 每分钟执行一次，检查大于60s的sql语句并杀死会话
     */
    @Async
    @Scheduled(cron = "0 */1 * * * ?")
    public void killSlowSql() {
        String execution = dao.selectSlowSql();
        if(!CommonUtils.isEmpty(execution)) {
            log.info(CommonUtils.formatNow() +" 删除慢sql：" + execution);
            dao.doKill(execution);
            log.info(CommonUtils.formatNow() +" 删除成功");
        }
    }

}

